package com.cangxuan.backend.dao;

import com.cangxuan.backend.entity.Goods;
import com.cangxuan.backend.entity.MarketGoods;
import com.cangxuan.backend.entity.User;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * user表操作接口
 */
@Mapper
public interface IUserDao {

    @Select("select * from user where mobile=#{mobile}")
    User selectByMobile(String mobile);

    @Select("select * from user where id=#{id}")
    User selectById(int id);

    @Insert("insert into user values(default,#{mobile},#{nickname},#{face},#{transPassword},#{realName}," +
            "#{idcard},#{blockchainAddress},now(),#{subscribeToTheSalesCalendar},#{displayMyGoods},#{displayMySellGoods},#{available},#{registrationId})")
    @Options(
            keyProperty = "id",
            useGeneratedKeys = true
    )
    int insert(User user);

    @Update("update user set nickname=#{nickname} where id=#{id}")
    int updateNickname(@Param("nickname") String nickname,
                       @Param("id") int id);


    int updateTransPassword(@Param("transPassword") String transPassword,
                            @Param("oldTransPassword") String oldTransPassword,
                            @Param("id") int id);

    @Update("update user set realName=#{realName},idcard=#{idcard} where id=#{id}")
    int updateRealNameAndIdcard(@Param("realName") String realName,
                                @Param("idcard") String idcard,
                                @Param("id") int id);

    @Update("update user set display_my_goods=#{displayMyGoods} where id=#{id}")
    int updateDisplayMyGoods(@Param("displayMyGoods") byte displayMyGoods,
                             @Param("id") int id);

    @Update("update user set display_my_sell_goods=#{displayMySellGoods} where id=#{id}")
    int updateDisplayMySellGoods(@Param("displayMySellGoods") byte displayMySellGoods,
                                 @Param("id") int id);

    @Update("update user set subscribe_to_the_sales_calendar=#{subscribeToTheSalesCalendar} where id=#{id}")
    int updateSubscribeToTheSalesCalendar(@Param("subscribeToTheSalesCalendar") Boolean TheSalesCalendar,
                                          @Param("id") int id);


    @Select("select count(0) from user_follow where user_id = #{userId} AND goods_id = #{goodsId}")
    int selectUserFollowGoods(@Param("userId") Integer userId, @Param("goodsId") Integer goodsId);

    @Insert("insert into user_follow values (default,#{userId},#{goodsId})")
    int addUserFollowGoods(@Param("userId") Integer userId, @Param("goodsId") Integer goodsId);

    @Delete("delete from user_follow where user_id = #{userId} AND goods_id = #{goodsId}")
    int cancelUserFollowGoods(@Param("userId") Integer userId, @Param("goodsId") Integer goodsId);


    List<MarketGoods> userFollowGoods(@Param("userId") Integer userId, @Param("pageIndex") Integer pageIndex);

    @Update("update user set face=#{facePath} where id=#{id}")
    int updateFacePath(@Param("facePath") String facePath,@Param("id") Integer id);

    @Update("update user set real_name=#{realName} ,idcard=#{idcard} where id=#{id}")
    int updateUserRealNameAndIDCard(@Param("realName") String realName,@Param("idcard") String idcard,@Param("id") Integer  id);
    @Update("update user set registration_id=#{registrationId} where id=#{userId}")
    int updateRegistrationId(@Param("registrationId")String registrationId,@Param("userId")int userId);

    @Select("select count(0) from user where id=#{userId} and idcard != ''")
    int selectUserCertified(@Param("userId") Long userId);


    @Update("update user set blockchain_address = #{account} where id = #{userId}")
    int updateBlockChain(@Param("userId") Long userId, @Param("account") String account);
}
